/**
 * Copyright (C) 2009 Olafur Gauti Gudmundsson, Hjortur Stefan Olafsson
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.gwt_control.client;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author Olafur Gauti Gudmundsson
 */
public abstract class AbstractController implements Controller {

    protected boolean feedModelEventsUpwards;
    protected Controller parentController;
    protected List<ModelListener> modelListeners;

    public AbstractController() {
        this(true);
    }

    public AbstractController( boolean feedModelEventsUpwards ) {
        this.feedModelEventsUpwards = feedModelEventsUpwards;
        this.modelListeners = new ArrayList<ModelListener>();
    }

    public void init( Controller parentController ) {
        this.parentController = parentController;
        init();
        if ( getModel() != null ) {
            // if this controller is model driven, we must initialise the model
            // before setting the layout
            getModel().init();
        } else {
            // else we set the layout directly here
            PageLayout layout = getLayout();
            ApplicationManager.view().setPageLayout(layout);
        }
    }

    protected abstract void init();

    protected abstract Model getModel();

    protected abstract PageLayout getLayout();

    public void handleApplicationEvent(ApplicationEvent event) {
        if ( parentController != null ) {
            parentController.handleApplicationEvent(event);
        }
    }

    public void sendModelReady() {
        for ( ModelListener listener : modelListeners ) {
            listener.onModelReady();
        }
        if ( feedModelEventsUpwards && parentController != null ) {
            parentController.sendModelReady();
        }
        
        PageLayout layout = getLayout();
        if ( layout != null ) {
            ApplicationManager.view().setPageLayout(layout);
        }
    }

    public void sendModelUpdated( ModelUpdateEvent event ) {
        for ( ModelListener listener : modelListeners ) {
            listener.onModelUpdate(event);
        }
        if ( feedModelEventsUpwards && parentController != null ) {
            parentController.sendModelUpdated(event);
        }
    }

    public void navigateTo(Controller controller) {
        parentController.handleApplicationEvent(new NavigationEvent(controller));
    }

    public void registerModelListener( ModelListener listener ) {
        modelListeners.add(listener);
    }

    public ViewLinkToken getViewLinkToken() {
        return null;
    }

}
